const fs = require("fs");
const { DATA_PATH } = require("../config/config.default.js");
const { getOptical } = require("../services/opticals.service.js");
class File {
    constructor(fileName) {
        this.fileName = fileName;
        this.modules = this.readFile();
    }
    readFile() {
        try {
            let rs = fs.readFileSync(this.fileName, "utf-8");
            if (!rs || rs == "" || rs == null || rs == undefined || rs == "{}") {
                return {}
            } else {
                let temp = JSON.parse(rs);
                if (temp['sites']) {
                    return temp['sites'];
                } else {
                    return [];
                }
            }
        } catch (error) {
            console.log(error);
            return []
        }
    }
    /* async readFile(){
        return new Promise((resolve, reject) => {
            try {
                fs.readFile(this.fileName, "utf-8", (err, data) => {
                    if (err) {
                        resolve({})
                    } else {
                        if (data == "") {
                            resolve({})
                        } else {
                            let temp = JSON.parse(data);
                            if (temp['sites']) {
                                resolve(temp['sites']);
                            } else {
                                resolve({})
                            }
                        }
                    }
                });
            } catch (error) {
                console.log(error);
                resolve({})
            }
        })
    } */
    writeFile(content) {
        try {
            var str = JSON.stringify({ sites: content });
            var rs = fs.writeFileSync(this.fileName, str)
            if (rs) {
                return true;
            } else {
                return false;
            }
        } catch (error) {
            return false
        }
    }
    eraseFile() {
        try {
            let rs = fs.unlinkSync(this.fileName);
            if (rs) {
                return true;
            } else {
                return false;
            }
        } catch (error) {
            console.log(error);
            return false
        }
    }
    getIdx(keyName) {
        if (!keyName || keyName == "" || keyName == undefined) {
            return -1;
        }
        for (var k in this.modules) {
            if (this.modules[k][keyName] == "") {
                return k;
            }
        }
        return -1;
    }
    //values为数组，需要更改的内容，keyName为需要替换的内容的键名。
    setValue({ values, keyName, insert = true }) {
        try {
            //this.modules = this.readFile();
            if (this.modules.length == 0) {
                if (values instanceof Array && values.length > 0) {
                    this.modules = values;
                } else {
                    return false;
                }
            } else {
                if (values instanceof Array && values.length > 0) {
                    if (keyName == "" || !keyName || keyName == undefined) {
                        return false;
                    }
                    //用来存储this.modules中的哪个元素是没有存储实际内容的。
                    let idx = this.getIdx(keyName);
                    /* for(var x=0;x<this.modules.length;x++){
                        if(this.modules[x][keyName] == ""){
                            idx = x;
                            break;
                        }
                    } */
                    for (var i = 0; i < values.length; i++) {
                        let sw = false;
                        for (var j = 0; j < this.modules.length; j++) {
                            //只要idx小于0时，并且this.modules某个元素的keyname项的值为空的时候，表示有空的值，可以添加
                            if (idx < 0 && this.modules[j][keyName] == "") {
                                idx = j;
                            }
                            if (values[i][keyName] == this.modules[j][keyName]) {
                                for (var k in values[i]) {
                                    this.modules[j][k] = values[i][k];
                                }
                                sw = true;
                                break;
                            }
                        }
                        if (!sw) {
                            //当insert为true时，表示直接往modules中添加元素，如果为False,则需要在对应的空白数据中，修改内容
                            if (insert) {
                                this.modules.push(values[i]);
                            } else {
                                if (idx >= 0) {
                                    for (let m in values[i]) {
                                        this.modules[idx][m] = values[i][m]
                                    }
                                    idx = this.getIdx(keyName);
                                }
                            }
                        }
                    }
                } else {
                    return false;
                }
            }
            this.writeFile(this.modules);
            return true;
        } catch (error) {
            return false;
        }
    }
    getValue() {
        try {
            let obj = this.readFile();
            if (Object.keys(obj).length == 0) {
                this.modules = [];
            } else {
                if (obj.sites) {
                    this.modules = obj.sites;
                } else {
                    this.modules = [];
                }
            }
            return this.modules;
        } catch (error) {
            return [];
        }
    }
}

class Gzgj extends File {
    constructor(fileName) {
        super(fileName)
    }
    setValue({ values, remove }) {
        try {
            //this.modules = this.readFile();
            //如果values为空，则返回false
            if (!values || !(values instanceof Object) || Object.keys(values).length == 0) {
                return false;
            }
            //如果文件中没有任何元素，则
            if (this.modules.length == 0) {
                this.modules.push(values);
            } else {
                let sw = false;
                for (var i = 0; i < this.modules.length; i++) {
                    if (this.modules[i]['Show_Name'] == values['Show_Name']) {
                        if (remove) {
                            this.modules.splice(i, 1);
                        } else {
                            values['Name'] = `BP_POI_GZGJ${i}`;
                            console.log("values=>", values);
                            this.modules.splice(i, 1, values);
                        }
                        sw = true;
                        break;
                    }
                }
                if (!sw) {
                    if (!remove) {
                        values['Name'] = `BP_POI_GZGJ${this.modules.length}`
                        this.modules.push(values);
                    }
                }
            }
            this.writeFile(this.modules);
            return true;
        } catch (error) {
            return false;
        }
    }
    /* getValueByCondition({ key, value }) {
        if (!key || key == "" || key == undefined) {
            return false;
        }
        if (!value || value == "" || value == undefined) {
            return false;
        }
        var list = this.readFile();
        if (list.length > 0) {
            for (var i = 0; i < list.length; i++) {
                if (list[i][key] == value) {
                    return list[i];
                }
            }
            return false;
        } else {
            return false;
        }
    } */
    getValueByCondition({ key, value }) {
        if (!key || key == "" || key == undefined) {
            return false;
        }
        if (!value || value == "" || value == undefined) {
            return false;
        }
        if (this.modules.length > 0) {
            for (var i = 0; i < this.modules.length; i++) {
                if (this.modules[i][key] == value) {
                    return JOSN.parse(JSON.stringify(this.modules[i]));
                }
            }
            return false;
        } else {
            return false;
        }
    }
}
class Poi extends File {
    constructor(fileName) {
        super(fileName)
    }
    readFile() {
        try {
            let rs = fs.readFileSync(this.fileName, "utf-8");
            if (!rs || rs == "" || rs == null || rs == undefined || !(JSON.parse(rs) instanceof Object) || Object.keys(JSON.parse(rs)).length == 0 || !(JSON.parse(rs)['sites'] instanceof Array) || JSON.parse(rs)['sites'].length == 0) {
                let arr = [];
                let gaojings = [];
                let companies = [];
                let lines = [];
                let boxes = [];
                let gzcx = { ID: `GZCX0`, Class: "GZ", Name: `BP_POI_GZG0`, Show_Name: "", Mesh: "SM_GZCX", X: 0, Y: 0, Z: "8000", URL: `0`, Build: "1", Visible: "1", Click: "0", Init: "0", R: "1", G: "0", B: "0", Color: "" };
                for (var i = 0; i < 30; i++) {
                    let dt = { ID: ``, Class: "GZ", Name: `BP_POI_GZGJ${i}`, Show_Name: "", Mesh: "SM_GZGJ", X: 0, Y: 0, Z: "8000000", URL: `0`, Build: "1", Visible: "1", Click: "0", Init: "0", R: "1", G: "0", B: "0", Color: "" };
                    arr.push(dt);
                    gaojings.push(dt)
                }
                for (var i = 0; i < 30; i++) {
                    let dt;
                    if (i == 0) {
                        dt = { "ID": "GDJ0", "Class": "Centre", "Name": `BP_POI_GDJ0`, "Show_Name": "呼和浩特供电公司", "Mesh": "SM_GDJ", "X": "0", "Y": "0", "Z": "15000", "URL": "http://ue.com:5500/btgdj", "Build": "1", "Visible": "0", "Click": "0", "Init": "0", "Color": "Green", "R": "0", "G": "0", "B": "1" }
                    } else {
                        dt = { "ID": "", "Class": "Line", "Name": `BP_POI_GDJ${i}`, "Show_Name": "", "Mesh": "SM_GDJ", "X": "0", "Y": "0", "Z": "0", "URL": "", "Build": "1", "Visible": "0", "Click": "0", "Init": "0", "Color": "Green", "R": "0", "G": "0", "B": "0", "stationId": "0" }
                    }
                    arr.push(dt);
                    companies.push(dt);
                }
                return {
                    gaojings,
                    companies,
                    lines,
                    boxes,
                    gzcx
                }
            } else {
                let temp = JSON.parse(rs);
                let list = temp['sites'];
                let modules = { gaojings: [], companies: [], lines: [], boxes: [] };
                for (var i = 0; i < list.length; i++) {
                    if (list[i]['Mesh'] == 'SM_GZGJ') {
                        modules.gaojings.push(list[i]);
                    } else if (list[i]['Mesh'] == "SM_GDJ") {
                        modules.companies.push(list[i]);
                    } else if (list[i]['Mesh'] == "SM_Line") {
                        modules.lines.push(list[i]);
                    } else if (list[i]['Mesh'] == "SM_Box") {
                        modules.boxes.push(list[i]);
                    } else if (list[i]['Mesh'] == "SM_GZCX") {
                        modules.gzcx = list[i];
                    }
                }
                if (modules.gaojings.length < 30) {
                    for (var i = modules.gaojings.length - 1; i < 30; i++) {
                        let dt = { ID: ``, Class: "GZ", Name: `BP_POI_GZGJ${i}`, Show_Name: "", Mesh: "SM_GZGJ", X: 0, Y: 0, Z: "8000", URL: `0`, Build: "1", Visible: "1", Click: "0", Init: "0", R: "1", G: "0", B: "0", Color: "" };
                        modules.gaojings.push(dt)
                    }
                }
                if (modules.companies.length < 30) {
                    let dt = { "ID": "", "Class": "Centre", "Name": `BP_POI_GDJ${i}`, "Show_Name": "", "Mesh": "SM_GDJ", "X": "0", "Y": "0", "Z": "0", "URL": "", "Build": "1", "Visible": "1", "Click": "0", "Init": "0", "Color": "Green", "R": "", "G": "", "B": "" }
                    modules.companies.push(dt);
                }
                if (!modules.gzcx || !(modules.gzcx instanceof Object) || Object.keys(modules.gzcx).length == 0) {
                    modules.gzcx = { ID: `GZCX0`, Class: "GZ", Name: `BP_POI_GZG0`, Show_Name: "", Mesh: "SM_GZCX", X: 0, Y: 0, Z: "8000", URL: `0`, Build: "1", Visible: "1", Click: "0", Init: "0", R: "1", G: "0", B: "0", Color: "" };
                }
                return modules
            }
        } catch (error) {
            console.log(error);
            let arr = [];
            let gaojings = [];
            let companies = [];
            let lines = [];
            let boxes = [];
            let gzcx = { ID: `GZCX0`, Class: "GZ", Name: `BP_POI_GZCX0`, Show_Name: "", Mesh: "SM_GZCX", X: 0, Y: 0, Z: "8000", URL: `0`, Build: "1", Visible: "1", Click: "0", Init: "0", R: "1", G: "0", B: "0", Color: "" };
            for (var i = 0; i < 30; i++) {
                let dt = { ID: ``, Class: "GZ", Name: `BP_POI_GZGJ${i}`, Show_Name: "", Mesh: "SM_GZGJ", X: 0, Y: 0, Z: "8000000", URL: `0`, Build: "1", Visible: "1", Click: "0", Init: "0", R: "1", G: "0", B: "0", Color: "" };
                arr.push(dt);
                gaojings.push(dt)
            }
            for (var i = 0; i < 30; i++) {
                let dt;
                if (i == 0) {
                    dt = { "ID": "GDJ0", "Class": "Centre", "Name": `BP_POI_GDJ0`, "Show_Name": "呼和浩特供电公司", "Mesh": "SM_GDJ", "X": "0", "Y": "0", "Z": "15000", "URL": "http://ue.com:5500/btgdj", "Build": "1", "Visible": "0", "Click": "0", "Init": "0", "Color": "Green", "R": "0", "G": "0", "B": "1" }
                } else {
                    dt = { "ID": "", "Class": "Line", "Name": `BP_POI_GDJ${i}`, "Show_Name": "", "Mesh": "SM_GDJ", "X": "0", "Y": "0", "Z": "0", "URL": "", "Build": "1", "Visible": "0", "Click": "0", "Init": "0", "Color": "Green", "R": "0", "G": "0", "B": "0", "stationId": "0" }
                }
                arr.push(dt);
                companies.push(dt);
            }
            return {
                gaojings,
                companies,
                lines,
                boxes,
                gzcx
            }
        }
    }
    writeFile() {
        try {
            let arr = [];
            if (this.modules.gaojings.length > 0) {
                for (let k in this.modules.gaojings) {
                    arr.push(this.modules.gaojings[k]);
                }
            }
            if (this.modules.companies.length > 0) {
                for (let k in this.modules.companies) {
                    arr.push(this.modules.companies[k]);
                }
            }
            if (this.modules.lines.length > 0) {
                for (let k in this.modules.lines) {
                    arr.push(this.modules.lines[k]);
                }
            }
            if (this.modules.boxes.length > 0) {
                for (let k in this.modules.boxes) {
                    arr.push(this.modules.boxes[k]);
                }
            }
            arr.push(this.modules.gzcx);
            var str = JSON.stringify({ sites: arr });
            var rs = fs.writeFileSync(this.fileName, str)
            if (rs) {
                return true;
            } else {
                return false;
            }
        } catch (error) {
            return false
        }
    }
    setSigleValue({ values, item, keys, remove }) {
        try {
            //this.modules = this.readFile();
            if (remove == undefined) {
                remove = false;
            }
            if (!keys || keys == "") {
                return false;
            }
            if (item != 'gaojings' && item != "companies" && item != "lines" && item != "boxes") {
                return false;
            }
            if (!values || values == undefined || values instanceof Object && Object.keys(values).length > 0) {
                let arr = this.modules[item];
                let sw = false;
                let emptyId = -1;
                for (var i = 0; i < arr.length; i++) {
                    if (emptyId < 0 && arr[i][keys] == "") {
                        emptyId = i;
                    }
                    if (values[keys] == arr[i][keys]) {
                        if (remove) {
                            arr.splice(i, 1);
                        } else {
                            values['Name'] = `BP_POI_GZGJ${i}`
                            arr.splice(i, 1, values);
                        }
                        sw = true;
                    }
                }
                if (emptyId >= 0 && !sw) {
                    values['Name'] = `BP_POI_GZGJ${emptyId}`
                    arr.splice(emptyId, 1, values);
                }
                return arr;
            } else {
                return false;
            }
        } catch (error) {
            console.log(error);
            return false
        }

    }
    setMutipleValue({ values, remove }) {
        try {
            if (remove == undefined) {
                remove = false;
            }
            if (!values || !(values instanceof Array) || values.length == 0) {
                return false;
            }
            if (item != 'gaojings' && item != "companies" && item != "lines" && item != "boxes") {
                return false;
            }
            for (var i = 0; i < values.length; i++) {
                this.modules[item] = this.setSigleValue(values[i].item, remove)
            }
            return this.modules[item];
        } catch (error) {
            console.log(error);
            return false;
        }

    }
    setGaojing({ values, key, remove }) {
        try {
            if (!key || key == undefined || key == "") {
                key = "Show_Name"
            }
            if (values instanceof Object && Object.keys(values).length > 0) {
                this.modules.gaojings = this.setSigleValue({ values: values, item: "gaojings", keys: key, remove: remove });
            }
        } catch (error) {
            console.log(error);
            return false;
        }
        this.writeFile()
    }
    setCompanies({ values, remove }) {
        try {
            if (values instanceof Object && Object.keys(values).length > 0) {
                this.modules.companies = this.setSigleValue({ values: companies, item: "companies", remove: remove });
            }
        } catch (error) {
            console.log(error);
        }

    }
    //remove为true代表删除
    setLines({ values, key, remove }) {
        try {
            if (!key || key == undefined || key == "") {
                key = "Show_Name"
            }
            /* if (values instanceof Object && Object.keys(values).length > 0) {
                this.modules.lines = this.setSigleValue({ values: values, item: "lines", keys: key, remove: remove });
            } */
            //this.modules = this.readFile();
            if (this.modules.lines.length > 0) {
                let update = false;
                for (var i = 0; i < this.modules.lines.length; i++) {
                    if (values[key] == this.modules.lines[i][key]) {
                        if (remove) {
                            this.modules.lines.splice(i, 1);
                        } else {
                            values['Name'] = `BP_POI_Line${i}`
                            this.modules.lines.splice(i, 1, values)
                        }
                        update = true;
                    }
                }
                if (!update) {
                    values['Name'] = `BP_POI_Line${this.modules.lines.length}`
                    this.modules.lines.push(values);
                }
                this.writeFile()
                return this.modules.lines;
            } else {
                values['Name'] = `BP_POI_Line0`
                this.modules.lines.push(values);
                this.writeFile();
                return this.modules.lines;
            }
        } catch (error) {
            console.log(error);
        }

    }
    hitLines({ values, key }) {
        try {
            if (!key || key == undefined || key == "") {
                key = "Show_Name"
            }
            if (values instanceof Object && Object.keys(values).length > 0) {
                //this.modules = this.readFile();
                let list = this.modules['lines'];
                if (list.length > 0) {
                    for (var i = 0; i < list.length; i++) {
                        if (list[i]['ID'] != "") {
                            if (values['Show_Name'] == list[i]['Show_Name']) {
                                list[i]['Click'] = "1";
                            } /* else {
                                list[i]['Click'] = "0"
                            } */
                        }
                    }
                    this.modules.lines = list;
                    this.writeFile();
                }
            }
        } catch (error) {
            console.log(error);
        }

    }
    setGzcx(gaojing) {
        try {
            //this.modules = this.readFile();
            this.modules.gzcx['Show_Name'] = gaojing.linkName;
            this.modules.gzcx['X'] = gaojing.X;
            this.modules.gzcx['Y'] = gaojing.Y;
            this.modules.gzcx['Z'] = gaojing.Z;
            this.modules.gzcx['URL'] = gaojing.URL;
            this.modules.gzcx['Click'] = "1";
            this.modules.gzcx['Init'] = "0";
            this.writeFile();
        } catch (error) {
            console.log(error);
        }
    }
    jcGzcx() {
        try {
            //this.modules = this.readFile();
            this.modules.gzcx['Init'] = "0";
            this.writeFile();
        } catch (error) {
            console.log(error);
        }
    }
    inArray(list, item, linkId, model) {
        if (!linkId || linkId == undefined || linkId <= 0) {
            return false;
        }
        if (list instanceof Array && list.length > 0) {
            for (var i = 0; i < list.length; i++) {
                //model为true代表arr为list,否则this.modules.boxex为list
                if (item['Show_Name'] == list[i]['Show_Name']) {
                    return true;
                }
            }
            return false;
        } else {
            return false;
        }
    }
    pushBox({ arr, linkId }) {
        if (!(arr instanceof Array) || arr.length == 0) {
            return false;
        }
        try {
            //this.modules = this.readFile();
            if (this.modules.boxes.length > 0) {
                for(var i=this.modules.boxes.length - 1;i>=0;i--){
                    if(this.modules.boxes[i]['linkId'] == linkId){
                        this.modules.boxes.splice(i,1);
                    }
                }
                for(var i=0;i<arr.length;i++){
                    let obj = { ID: `BOX${i}`, Class: "Box", Name: `BP_POI_Box${i}`, Show_Name: arr[i]['BoxName'], Mesh: "SM_Box", X: "0", Y: "0", Z: "0", URL: `http://ue.com:5500/jth?boxId=${arr[i]['ID']}`, Build: "1", Visible: "0", Click: "0", Init: "0", Color: "Green", R: "0", G: "0", B: "0", linkId: linkId };
                    this.modules.boxes.push(obj)
                }
            } else {
                for (var i = 0; i < arr.length; i++) {
                    let obj = { ID: `BOX${i}`, Class: "Box", Name: `BP_POI_Box${i}`, Show_Name: arr[i]['BoxName'], Mesh: "SM_Box", X: "0", Y: "0", Z: "0", URL: `http://ue.com:5500/jth?boxId=${arr[i]['ID']}`, Build: "1", Visible: "0", Click: "0", Init: "0", Color: "Green", R: "0", G: "0", B: "0", linkId: linkId };
                    this.modules.boxes.push(obj);
                }
            }
            this.writeFile();
        } catch (error) {
            return false;
        }
    }
    clearBox({linkId }) {
        this.modules = this.readFile()
        try {
            if (this.modules.boxes.length == 0) {
                return true;
            }
            console.log(this.modules.boxes.length);
            for (var i = this.modules.boxes.length - 1; i >=0; i--) {                
                if(this.modules.boxes[i]['linkId'] == linkId){
                   console.log(true);
                    this.modules.boxes.splice(i,1);
                }
            }
            this.writeFile();
            return true;
        } catch (error) {
            console.log(error);
            return;
        }
    }
    clearGDJ({ linkId }) {
        if (!linkId || linkId <= 0 || linkId == undefined) {
            return false;
        }
        try {
            //this.modules = this.readFile();
            if (this.modules['companies'].length > 1) {
                for (var i = 1; i < 30; i++) {
                    if (this.modules['companies'][i]['linkId'] == linkId) {
                        this.modules['companies'][i]['Show_Name'] = "";
                        this.modules['companies'][i]['linkId'] = "0";
                        this.modules['companies'][i]['URL'] = "";
                    }
                }
            }
            this.writeFile();
        } catch (error) {
            console.log(error);
        }
    }
    pushGDJ({ values, linkId }) {
        try {
            if (!linkId || linkId <= 0 || linkId == undefined) {
                return false;
            }
            if (values instanceof Array && values.length > 0) {
                //this.modules = this.readFile();
                if (this.modules['companies'].length > 1) {
                    for (var i = 1; i < 30; i++) {
                        if (this.modules['companies'][i]['linkId'] == "0") {
                            if (values[i - 1]) {
                                this.modules['companies'][i]['ID'] = `GDJ${i}`;
                                this.modules['companies'][i]['Show_Name'] = values[i - 1]['BoxName'];
                                this.modules['companies'][i]['linkId'] = linkId;
                                this.modules['companies'][i]['URL'] = `http://ue.com:5500/jth?boxId=${values[i - 1]['ID']}`;
                            }
                        }
                    }
                }
                this.writeFile();
            } else {
                return false;
            }
        } catch (error) {
            console.log(error);
        }
    }
    insertBDZ({value,remove}){
        if(!remove || remove == "" || remove == undefined){
            remove == false;
        }
        if(value instanceof Object && Object.keys(value).length > 0){
            console.log("lenght=?",this.modules.companies.length);
            if(this.modules.companies.length > 0){
                let empty = -1;
                console.log("大于0");
                for(var i=0;i<this.modules.companies.length;i++){
                    if(empty < 0 && this.modules.companies[i]['Show_Name'] == ""){
                        empty = i;
                    }
                    console.log("empty=>",empty,"modules.stationsid=>",this.modules.companies[i]['stationId'],"stationId=>",value['stationId']);
                    //找到名称相同的记录，如果remove为True时，相应键名清零，否则修改相就键名、
                    if(this.modules.companies[i]['stationId'] == value['stationId']){
                        if(remove){
                            this.modules.companies[i]['Show_Name'] = "";
                            this.modules.companies[i]['X'] = "0";
                            this.modules.companies[i]['Y'] = "0";
                            this.modules.companies[i]['Z'] = "0";
                            this.modules.companies[i]['URL'] = "";
                            this.modules.companies[i]['R'] = "0";
                            this.modules.companies[i]['G'] = "0";
                            this.modules.companies[i]['B'] = "0";
                            this.modules.companies[i]['stationId'] = "0";
                        }else{
                            console.log(333);
                            this.modules.companies[i]['ID'] = `GDJ${i}}`;
                            this.modules.companies[i]['Name'] = `BP_POI_GDJ${i}`;
                            this.modules.companies[i]['Mesh'] = "SM_GDJ";
                            this.modules.companies[i]['Show_Name'] = value['stationName'];
                            this.modules.companies[i]['X'] = "0";
                            this.modules.companies[i]['Y'] = "0";
                            this.modules.companies[i]['Z'] = "0";
                            this.modules.companies[i]['URL'] = `http://ue.com:5500/bdz?stationId=${value['stationId']}`;
                            this.modules.companies[i]['R'] = value['R'];
                            this.modules.companies[i]['G'] = value['G'];
                            this.modules.companies[i]['B'] = value['B'];
                            this.modules.companies[i]['stationId'] = value['stationId'];
                        }
                        this.writeFile();
                        return;
                    }
                }
                if(empty > 0){
                    this.modules.companies[empty]['ID'] = `GDJ${empty}`;
                    this.modules.companies[empty]['Name'] = `BP_POI_GDJ${i}`;
                    this.modules.companies[empty]['Mesh'] = "SM_GDJ";
                    this.modules.companies[empty]['Show_Name'] = value['stationName'];
                    this.modules.companies[empty]['X'] = "0";
                    this.modules.companies[empty]['Y'] = "0";
                    this.modules.companies[empty]['Z'] = "0";
                    this.modules.companies[empty]['URL'] = ``;
                    this.modules.companies[empty]['R'] = value['R'];
                    this.modules.companies[empty]['G'] = value['G'];
                    this.modules.companies[empty]['B'] = value['B'];
                    this.modules.companies[empty]['URL'] = `http://ue.com:5500/bdz?stationId=${value['stationId']}`;
                    this.modules.companies[empty]['stationId'] = value['stationId']
                }
            }else{
                if(!remove){
                    let len = this.modules.companies.length;
                    let obj = {
                        ID:`GDJ${len}`,
                        Class:"Line",
                        Name:`BP_POI_GDJ${len}`,
                        Show_Name:value['stationName'],
                        Mesh:"SM_GDJ",
                        X:"0",
                        Y:"0",
                        Z:"0",
                        URL:``,
                        Build:"1",
                        Visible:"0",
                        Click:"0",
                        Init:"0",
                        Color:"Green",
                        R:value['R'],
                        G:value['G'],
                        B:value['B'],
                        stationId:value['stationId']
                    }
                    this.modules.companies.push(obj);
                }
            }
            this.writeFile();
        }else{
            return false;
        }
    }
    removeLine() {
        this.modules.lines = [];
    }

    setValue({ gaojings, companies, lines, boxes, keyName, keyword, insert = true }) {
        try {
            //this.modules = this.readFile();
            if (gaojings instanceof Object && Object.keys(gaojings).length > 0 && gaojings.content instanceof Object && Object.keys(gaojings.content).length > 0) {
                this.modules['gaojings'] = this.setSigleValue({ values: gaojings.content, item: 'gaojings', remove: gaojings.remove });
            }
            if (companies instanceof Object && Object.keys(companies).length > 0 && companies.content instanceof Object && Object.keys(companies.content).length > 0) {
                this.modules['companies'] = this.setSigleValue({ values: companies.content, item: "companies", remove: companies.remove });
            }
            this.writeFile(this.modules);
            return true;
        } catch (error) {
            return false;
        }
    }
    getValue() {
        try {
            let obj = this.readFile();
            if (Object.keys(obj).length == 0) {
                this.modules = [];
            } else {
                if (obj.sites) {
                    this.modules = obj.sites;
                } else {
                    this.modules = [];
                }
            }
            return this.modules;
        } catch (error) {
            return [];
        }
    }
    getValueByCondition({ item, key, value }) {
        if (!item || item == undefined || item == "") {
            return false;
        }
        if (!key || key == "" || key == undefined) {
            return false;
        }
        if (!value || value == "" || value == undefined) {
            return false;
        }
        //this.modules = this.readFile();
        let list = this.modules[item];
        if (list.length > 0) {
            for (var i = 0; i < list.length; i++) {
                if (list[i][key] == value) {
                    return list[i];
                }
            }
            return false;
        } else {
            return false;
        }
    }
}

class Updata extends File {
    constructor(fileName) {
        super(fileName)
    }
    async readFile() {
        try {
            let rs = fs.readFileSync(this.fileName, "utf-8");
            if (!rs || rs == "" || rs == null || rs == undefined || rs == "{}") {
                return { sites: { Updata: "0" } }
            } else {
                let temp = JSON.parse(rs);
                if (temp['sites']) {
                    return temp['sites'];
                } else {
                    return { sites: { Updata: "0" } };
                }
            }
        } catch (error) {
            console.log(error);
            return { sites: { Updata: "0" } }
        }
    }
    /* async readFile(){
        try {
            let rs = fs.readFileSync(this.fileName, "utf-8");
            if (!rs || rs == "" || rs == null || rs == undefined || rs == "{}") {
                return {}
            } else {
                let temp = JSON.parse(rs);
                if (temp['sites']) {
                    return temp['sites'];
                } else {
                    return [];
                }
            }
        } catch (error) {
            console.log(error);
            return []
        }
    } */
    async writeFile({ num, time, delay }) {
        try {
            if (!time || time <= 0) {
                time = 500;
            }
            var content = {}
            if (num == 0 || !num || num == undefined) {
                content.Updata = "0"
            } else {
                content.Updata = "1"
            }
            console.log("update->content=>", num);
            var str = JSON.stringify({ sites: content });
            fs.writeFileSync(this.fileName, str)
            setTimeout(() => {
                let obj = { sites: { Updata: "0" } };
                let objstr = JSON.stringify(obj);
                fs.writeFileSync(this.fileName, objstr);
            }, time);
            /* setTimeout(() => {
                let obj = { sites: { Updata: "0" } };
                let objstr = JSON.stringify(obj);
                fs.writeFileSync(this.fileName, objstr);
            }, 1000); */
        } catch (error) {
            console.log(error);
            return false
        }
    }
}
class Nodes {
    constructor() {
        this.modules = [];
        this.fileName = ""
    }
    static readFile(linkId) {
        if (!linkId || linkId < 0) {
            return false;
        }
        this.getFileName(linkId)
        try {
            let rs = fs.readFileSync(this.fileName, "utf-8");
            if (!rs || rs == "" || rs == null || rs == undefined || rs == "{}") {
                return []
            } else {
                let temp = JSON.parse(rs);
                if (temp['sites']) {
                    return temp['sites'];
                } else {
                    return [];
                }
            }
        } catch (error) {
            console.log(error);
            return []
        }
    }
    static writeFile(linkId, content) {
        if (!linkId || linkId < 0) {
            return false;
        }
        this.getFileName(linkId)
        try {
            var str = JSON.stringify({ sites: content, status: "Color" });
            fs.writeFileSync(this.fileName, str)
            return true;
        } catch (error) {
            return false
        }
    }
    static eraseFile(linkId) {
        if (!linkId || linkId < 0) {
            return false;
        }
        this.getFileName(linkId)
        try {
            let rs = fs.unlinkSync(this.fileName);
            if (rs) {
                return true;
            } else {
                return false;
            }

        } catch (error) {
            console.log(error);
            return false
        }
    }
    static getFileName(linkId) {
        this.fileName = `${DATA_PATH}Js_Node${linkId}.json`;
    }
    static setValue({ linkId, values, wkey, wvalue, defaults }) {
        if (!linkId || linkId < 0) {
            return false;
        }
        try {
            this.modules = this.readFile(linkId);
            if (this.modules.length == 0) {
                return false;
            }
            if (!values || values == "" || values == undefined) {
                return false;
            }
            if (defaults) {
                for (var k in values) {
                    this.modules[0][k] = values[k];
                }
            } else {
                if (!wkey || wkey == "" || wkey == undefined) {
                    return false;
                }
                if (!wvalue || wvalue == "" || wvalue == undefined) {
                    return false;
                }
                for (let i = 0; i < this.modules.length; i++) {
                    if (this.modules[i][wkey] == wvalue) {
                        for (var k in values) {
                            this.modules[i][k] = values[k];
                        }
                    }
                }
            }
            let res = this.writeFile(this.modules);
            if (res) {
                return true;
            } else {
                return false;
            }
        } catch (error) {
            return false;
        }
    }
    static getValue(linkId) {
        this.getFileName(linkId)
        if (!linkId || linkId < 0) {
            return false;
        }
        try {
            let obj = this.readFile();
            if (Object.keys(obj).length == 0) {
                this.modules = [];
            } else {
                if (obj.sites) {
                    this.modules = obj.sites;
                } else {
                    this.modules = [];
                }
            }
            return this.modules;
        } catch (error) {
            return [];
        }
    }
    static async changeColor({ linkId, linkName, num, model }) {
        if (!linkId || linkId <= 0 || linkId == undefined) {
            return false;
        }
        if (num < 0 || num == undefined) {
            return false;
        }
        let start = 0;
        var opticals = await getOptical({ linkName });
        if (!opticals) {
            return false;
        }
        for (var j = 0; j < opticals.length; j++) {
            if (num == opticals[j]['opticalId']) {
                start = j;
            }
        }
        var modules = this.readFile(linkId);
        if (modules) {
            return false;
        }
        if (!(modules instanceof Array) || modules.length == 0) {
            return false;
        }
        //model = 1为设置告警，R为0.2，否则为原本颜色
        if (model == 1) {
            modules[start]['R'] = "0.2";
            modules[start]['G'] = "0";
            modules[start]['B'] = "0";
            if (modules[start + 1]) {
                modules[start + 1]['R'] = "0.2";
                modules[start + 1]['G'] = "0";
                modules[start + 1]['B'] = "0";
            }
            if (modules[start + 2]) {
                modules[start + 2]['R'] = "0.2";
                modules[start + 2]['G'] = "0";
                modules[start + 2]['B'] = "0";
            }
        } else {
            for (var k = 0; k < modules.length; k++) {
                modules[k]['R'] = opticals[k]['R'];
                modules[k]['G'] = opticals[k]['G'];
                modules[k]['B'] = opticals[k]['B'];
            }
        }
        this.writeFile({ linkId, modules });
    }
    static createLine({ linkId, linkName, arr }) {
        if (!linkId || linkId <= 0) {
            return false;
        }
        if (!linkName || linkName == "" || linkName == undefined) {
            return false;
        }
        if (!(arr instanceof Array) || arr.length == 0) {
            return false;
        }
        this.fileName = `${DATA_PATH}Js_Node${linkId}.json`;
        let list = [];
        for (var i = 0; i < arr.length; i++) {
            let obj;
            if (arr[i]['nodeType'] == "SM_Well") {
                obj = { ID: `L${linkId}N${i}`, class: "Node", Name: `BP_Node_L${linkId}N${i}`, Show_Name: linkName, Mesh: arr[i]['nodeType'], X: 3836187.737 * (0 + arr[i]['longitude'] - 111.67240) / 0.86915612, Y: -7091297.591 * (0 + arr[i]['latitude'] - 40.79964) / 1.20283173, Z: "4000", Build: "1", Click: "0", R: arr[i]['R'], G: arr[i]['G'], B: arr[i]['B'] };
            } else {
                obj = { ID: `L${linkId}N${i}`, class: "Node", Name: `BP_Node_L${linkId}N${i}`, Show_Name: linkName, Mesh: arr[i]['nodeType'], X: 3836187.737 * (0 + arr[i]['longitude'] - 111.67240) / 0.86915612, Y: -7091297.591 * (0 + arr[i]['latitude'] - 40.79964) / 1.20283173, Z: "8000", Build: "1", Click: "0", R: arr[i]['R'], G: arr[i]['G'], B: arr[i]['B'] };
            }
            list.push(obj);
        }
        let res = this.writeFile(linkId, list);
        if (res) {
            return true;
        } else {
            return false;
        }
    }
}
module.exports = {
    Gzgj,
    Poi,
    Updata,
    Nodes
}